<?php
namespace Admin\Controller;

use Admin\Util;

use Think\Controller;

class PicController extends Controller {

    public function Index(){
        $LS = Service('Pic','home');
        $where =array();
        if($_POST['PAGE']){
            $where['PAGE'] =  $_POST['PAGE'] ;

        }
        if($_POST['TYPE']){
            $where['TYPE'] =  $_POST['TYPE'] ;
        }

        $data = $LS->query($where);
        $this->data  = $data;
        $this->display();
    }

    public function add(){
        $this->display();
    }
    public function save(){
        $img = new \Util\IMGUpload(true);
        $img->rootPath =  './Uploads/'; //保存根路径
        $img->savePath = 'Norms/'; //保存路径
        $img->ThumbSize = array(500,500);
        $img->ThumbPath = 'NormsThumb/';
        $data = $img->upload();
        if($data){
            $datasave=array(
                'ID'=>uniqid(),
                'PIC_PATH' => $data['PIC_PATH']['path'],
                'URL'=>$_POST['URL'],
                'CREATETIME'=>date('Y-m-d H:i:s'),
                'UPDATETIME'=>date('Y-m-d H:i:s'),
                'SORT'=>$_POST['SORT'],
                'TYPE'=>$_POST['TYPE'],
                'PAGE'=>$_POST['PAGE'],
            );
            $gps = Service('Pic','home');
            $gps->insert($datasave);
            redirect(U('Pic/index')."?fid={$_POST['UNID']}&name={$_GET['name']}");
        }else{
            $this->error('亲，上传失败,上传的图片不能大于1MB！！');
        }
    }
    public function del(){
        $ID = $_POST['id'];
        $LS = Service('Pic','home');
        $OK = $LS->remove(array('ID'=>$ID));
        echo $OK;
    }

    public function updateindex(){
        $LS = Service('Pic','home');
        $where['ID'] = $_GET['ID'];
        $DATA = $LS->find($where);
        $this->DATA = $DATA;
        $this->display('edit');
    }

    public  function updata(){
        $LS = Service('Pic','home');
        $where['ID'] = $_POST['ID'];
        unset($_POST['ID']);
        $save = $_POST;
        $save['UPDATETIME'] = date('Y-m-d H:i:s');

        $OK = $LS->update($where,$save);
        if($OK){
            $this->success('编辑成功');
        }else{
            $this->error('编辑失败');
        }

    }

    public function  AddAllIndex(){
        $ALLDATA = S('Picdata');
//        dump($ALLDATA);
        $this->DATA = $ALLDATA;
        $this->display('AddAllIndex');
    }

    public function import() {
        if (!isset($_FILES['execel']) || $_FILES["import"]["error"] != 0) {
            exit('File Not Exit!');
        }
        $extension = explode('.', $_FILES['execel']['name']);

        $res = \Admin\Util\ExcelUtil::importExecl($_FILES['execel']['tmp_name'], $extension[1]);

        S('Picdata', $res);
        $this->redirect('Pic/importHandle');
    }

    public function importHandle() {
        $res = S('Picdata');
        unset($res[0]);
        $ALLdata = array();
        $HOME_TYPE = C('HOME_TYPE');
        $PAGE = C('PAGE');
        foreach ($res as $key => $value) {
            $data['ID'] = uniqid();
            $data["PIC_PATH"] = $value[0];
            $data['URL'] = $value[1];
            $data['CREATETIME'] = date('Y-m-d H:i:s');
            $data['UPDATETIME'] = date('Y-m-d H:i:s');
            $data['SORT'] = $value[2];
            $data['TYPE'] = $value[3];
            $data['TYPES'] = $HOME_TYPE[$value[3]];
            $data['PAGE'] = $value[4];
            $data['PAGES'] = $PAGE[$value[4]];
            $ALLdata[$key]['data'] = $data;
            $ALLdata[$key]['STATE'] = 1;
        }
        S('Picdata', $ALLdata);
        $this->redirect('Pic/AddAllIndex');

    }

    public function importSQL(){
        $DATA = S('Picdata');
        $LS = Service('Pic','home');
        foreach($DATA as $k=>$v){
            $temp = null;
            if($v['STATE'] != 0){
                $temp = $v['data'];
                unset($temp['TYPES']);
                unset($temp['PAGES']);
                $OK = $LS->insert($temp);
                if($OK){
                    $DATA[$k]['STATE'] = 0;
                }else{
                    $DATA[$k]['STATE'] = -1;
                }
            }
        }
        S('Picdata',NULL);
        S('Picdata',$DATA);
        $this->redirect('Pic/AddAllIndex');
    }
    public function clear() {
        S('Picdata', NULL);
        $this->redirect('Pic/AddAllIndex');
    }

}